﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.ClientDisplayMechanics {
    import flash.events.*;
    import Engine.Events.*;
    import flash.geom.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import flash.utils.*;
    import Classes.*;
    import Classes.effects.*;

    public class MultiPickMechanic implements IClientGameMechanic, IEdgeModifier {

        protected static const MECHANIC_PICKS:String = "picks";

        protected var m_owner:MechanicMapResource = null;
        protected var m_config:MechanicConfigData = null;
        protected var m_multipick:MultiPickEffect = null;
        protected var m_isDirty:Boolean = false;
        protected var m_gameMode:String = null;
        protected var m_pickToMechanic:Dictionary = null;
        protected var m_dirtyPick:String = null;
        protected var m_dirtyMechanic:MechanicConfigData = null;

        public function updateDisplayObject(_arg1:Number):void{
            var _local2:String = getQualifiedClassName(Global.world.getTopGameMode());
            if (((this.m_isDirty) || (!((this.m_gameMode == _local2))))){
                this.refreshGameEventMechanicPicks();
                this.m_gameMode = _local2;
                this.m_isDirty = false;
            };
            this.m_multipick.reattach();
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            var _local3:XML;
            var _local4:String;
            var _local5:String;
            var _local6:String;
            if (this.m_dirtyMechanic){
                this.m_dirtyMechanic.resetMechanicToXMLConfig();
                this.m_dirtyMechanic = null;
            };
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = null;
            this.m_pickToMechanic = null;
            this.m_dirtyPick = null;
            this.m_dirtyMechanic = null;
            this.m_multipick = null;
            this.m_gameMode = getQualifiedClassName(Global.world.getTopGameMode());
            if (this.m_owner){
                this.m_config = _arg2;
                this.m_pickToMechanic = new Dictionary();
                this.m_multipick = new MultiPickEffect(this.m_owner);
                this.m_multipick.setOption(MultiPickEffect.OPTION_ACTION_ON_SELECT, true);
                this.m_multipick.addEventListener(LoaderEvent.LOADED, this.onMultiPickLoaded, false, 0, true);
                if (_arg2.rawXMLConfig.hasOwnProperty(MECHANIC_PICKS)){
                    for each (_local3 in _arg2.rawXMLConfig.picks.pick) {
                        _local4 = String(_local3.@name);
                        _local5 = String(_local3.@type);
                        _local6 = String(_local3.@modifier);
                        if (!_local6.length){
                            _local6 = null;
                        };
                        this.m_multipick.create(_local4, _local5, _local6);
                        this.m_multipick.viewable(_local4, false);
                    };
                };
                this.refreshGameEventMechanicPicks();
                this.m_multipick.reattach();
                this.m_owner.addEventListener(GenericObjectEvent.MECHANIC_DATA_CHANGED, this.onMechanicDataChanged, false, 0, true);
                this.m_multipick.addEventListener(MouseEvent.MOUSE_UP, this.onMultiPickPickChanged, false, 0, true);
                this.m_owner.addEventListener(GameObjectEvent.STATE_CHANGE, this.onStateChanged, false, 0, true);
            };
        }
        protected function onMultiPickLoaded(_arg1:LoaderEvent):void{
            if (this.m_dirtyPick){
                this.m_multipick.active = this.m_dirtyPick;
            };
            this.m_multipick.removeEventListener(LoaderEvent.LOADED, this.onMultiPickLoaded);
        }
        protected function onMechanicDataChanged(_arg1:GenericObjectEvent):void{
            if (this.m_config.type != _arg1.obj){
                this.m_isDirty = true;
            };
        }
        protected function onStateChanged(_arg1:GameObjectEvent):void{
            this.m_multipick.reattach();
        }
        protected function onMultiPickPickChanged(_arg1:MouseEvent):void{
            var _local3:MechanicConfigData;
            var _local2:String = this.m_multipick.active;
            if (this.m_multipick.exists(_local2)){
                _local3 = (this.m_pickToMechanic[_local2] as MechanicConfigData);
                if (this.m_dirtyMechanic){
                    this.m_dirtyMechanic.resetMechanicToXMLConfig();
                    this.m_dirtyMechanic = null;
                };
                if (_local3){
                    _local3.priority = int.MAX_VALUE;
                };
                this.m_dirtyMechanic = _local3;
                this.m_dirtyPick = _local2;
            };
        }
        protected function refreshGameEventMechanicPicks(_arg1:String=null):void{
            var _local2:int;
            var _local3:int;
            var _local4:String;
            var _local5:String;
            var _local6:Dictionary;
            var _local7:Array;
            var _local8:String;
            var _local9:MechanicConfigData;
            var _local10:String;
            var _local11:IActionGameMechanic;
            var _local12:String;
            var _local13:Boolean;
            var _local14:Boolean;
            var _local15:int;
            var _local16:int;
            if (this.m_owner){
                if (((!(_arg1)) || (((_arg1) && (!(_arg1.length)))))){
                    _local8 = getQualifiedClassName(Global.world.getTopGameMode());
                    _arg1 = _local8.split("::").pop();
                };
                _local2 = 0;
                _local3 = 0;
                _local4 = null;
                _local5 = null;
                _local6 = this.m_owner.actionMechanics;
                _local7 = (_local6[_arg1] as Array);
                if (((_local7) && (_local7.length))){
                    for each (_local9 in _local7) {
                        _local10 = _local9.type;
                        _local11 = (MechanicManager.getInstance().getMechanicInstance(this.m_owner, _local10, _arg1) as IActionGameMechanic);
                        if ((_local11 is IMultiPickSupporter)){
                            _local12 = (_local11 as IMultiPickSupporter).getPick();
                            if (this.m_multipick.exists(_local12)){
                                _local13 = this.m_multipick.isViewable(_local12);
                                _local14 = _local11.hasOverrideForGameAction(_arg1);
                                _local15 = 0;
                                this.m_multipick.viewable(_local12, _local14);
                                this.m_pickToMechanic[_local12] = _local9;
                                if (((!(_local13)) && (_local14))){
                                    _local15 = int(_local9.rawXMLConfig.@priority);
                                    if (_local15 > _local3){
                                        _local3 = _local15;
                                        _local4 = _local12;
                                    };
                                };
                                if (_local14){
                                    _local15 = int(_local9.rawXMLConfig.@priority);
                                    if (_local15 > _local2){
                                        _local2 = _local15;
                                        _local5 = _local12;
                                    };
                                };
                            };
                        };
                    };
                };
                if (((((this.m_dirtyMechanic) && (this.m_dirtyPick))) && (this.m_multipick.isViewable(this.m_dirtyPick)))){
                    _local5 = this.m_dirtyPick;
                    _local16 = int(this.m_dirtyMechanic.rawXMLConfig.@priority);
                    if (((_local4) && ((_local3 > _local16)))){
                        _local5 = _local4;
                    };
                };
                if (((!((_local5 == this.m_dirtyPick))) && (this.m_dirtyMechanic))){
                    this.m_dirtyMechanic.resetMechanicToXMLConfig();
                    this.m_dirtyMechanic = null;
                };
                this.m_dirtyPick = _local5;
                this.m_multipick.active = _local5;
            };
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }
        public function isPixelInside(_arg1:Point):Boolean{
            var _local2:Rectangle = this.m_multipick.getRect(this.m_owner.displayObject.parent);
            var _local3:Boolean = _local2.containsPoint(_arg1);
            return (_local3);
        }
        public function getFloatOffset():int{
            return (this.m_multipick.getFloatOffset());
        }

    }
}//package Mechanics.ClientDisplayMechanics 
